package com.hwl.server.controller;


import com.hwl.server.entity.Admin;
import com.hwl.server.entity.Role;
import com.hwl.server.pojo.RespBean;
import com.hwl.server.service.AdminService;
import com.hwl.server.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-11-28
 */
@Slf4j
@Api(tags = "AdminController")
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Resource
    private AdminService adminService;
    @Resource
    private RoleService roleService;

    @ApiOperation("操作员管理")
    @GetMapping("/")
    public List<Admin> getAllAdmin(String keyword) {
        return adminService.getAllAdmin(keyword);
    }

    @ApiOperation("更新操作员信息")
    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return RespBean.success("更新操作员成功");
        }
        return RespBean.error("更新操作员失败");
    }

    @ApiOperation("删除操作员")
    @DeleteMapping("/{id}")
    public RespBean delAdmin(@PathVariable Integer id) {
        if (adminService.removeById(id)) {
            return RespBean.success("删除操作员成功！");
        }
        return RespBean.error("删除操作员失败！");
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles() {
        return roleService.list();
    }

    @ApiOperation("更新操作员角色")
    @PutMapping("/role")
    public RespBean updateAdminRole(Integer adminId, Integer[] rids) {
        return adminService.updateAdminRole(adminId, rids);
    }

}
